package	com.sdry.service.impl.llm;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sdry.model.llm.LlmPrepaidExpenses;
import com.sdry.service.llm.LlmPrepaidExpensesService;
import com.sdry.mapper.llm.LlmPrepaidExpensesMapper;

/**
 *@Description: 摊销费用
 *@Author llm
 *@Date 2019-09-29 11:05:20
 *@version HAJY_1.2
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class LlmPrepaidExpensesServiceImpl implements LlmPrepaidExpensesService {
	@Resource 
	private LlmPrepaidExpensesMapper llmPrepaidExpensesMapper;
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	public LlmPrepaidExpenses queryById(Long id) {
		return llmPrepaidExpensesMapper.queryById(id);
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<LlmPrepaidExpenses> queryAllByMution(LlmPrepaidExpenses param) {
		return llmPrepaidExpensesMapper.queryAllByMution(param);
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<LlmPrepaidExpenses> findPageByMution(LlmPrepaidExpenses param) {
		return llmPrepaidExpensesMapper.findPageByMution(param);
	}
	/** 
	 * 条件查询记录数
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public Integer selectCountByMution(LlmPrepaidExpenses param) {
		return llmPrepaidExpensesMapper.selectCountByMution(param);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @return 主键id
	*/
	public Long insert(LlmPrepaidExpenses param) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = format.format(new Date());
		param.setCreateDate(time);
		int one = param.getMoney().compareTo(new BigDecimal(500));
		int num = 0;
		if(one == -1){
			num = 3;
		}else{
			int two = param.getMoney().compareTo(new BigDecimal(3000));
			if(two == 1){
				num = 12;
			}else{
				num = 6;
			}
		}
		param.setAmortizationIn(num);
		BigDecimal monthMoney = param.getMoney().divide(new BigDecimal(num), 2, BigDecimal.ROUND_HALF_UP);
		param.setAmortizationCost(monthMoney);
		param.setAmortiztionInCurrentMonth(monthMoney);
		param.setWarehouseId(14L);
		//计算待摊期间
		Calendar cale = null;  
        cale = Calendar.getInstance();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
		try {
			date = sdf.parse(param.getDateOfOccurrence());
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cale.setTime(date);//设置日历时间
        cale.add(Calendar.MONTH,num);//在日历的月份上增加6个月
        Date oldDate = cale.getTime();
        String oldTime = sdf.format(oldDate);
        param.setDuringOfPrepaid(param.getDateOfOccurrence() + "~" + oldTime);
		return llmPrepaidExpensesMapper.insert(param);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @return 影响行数
	*/
	public Integer update(LlmPrepaidExpenses param) {
		return llmPrepaidExpensesMapper.update(param);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	public Integer delete(String ids) {
		return llmPrepaidExpensesMapper.delete(ids);
	}
}
